from datetime import datetime

from sqlalchemy_serializer import SerializerMixin

from app import dBSession
from app.Models.BaseModel import BaseModel
from app.Models.Model import FloorVehicle
from app.Vendor.Decorator import classTransaction


class FloorVehicles(FloorVehicle, BaseModel, SerializerMixin):

    @classTransaction
    def get_one(self, vehicle):
        res = dBSession.query(FloorVehicle).filter(FloorVehicle.id == vehicle.id).first()
        if not res:
            return None
        res = res.to_dict()
        return res

    @classTransaction
    def get_one_by_vin(self, vin):
        res = dBSession.query(FloorVehicle).filter(FloorVehicle.vin == vin).first()
        return res

    # 增加
    @classTransaction
    def add(self, vehicle):
        vehicle.created_at = datetime.now()
        dBSession.add(vehicle)
        dBSession.flush()
        return vehicle.id

    @classTransaction
    def edit(self, vehicle):
        update_data = {"updated_by": vehicle.updated_by, "updated_at": datetime.now()}
        if vehicle.serno:
            update_data["serno"] = vehicle.serno
        if vehicle.vin:
            update_data["vin"] = vehicle.vin
        if vehicle.vehicle_part:
            update_data["vehicle_part"] = vehicle.vehicle_part
        if vehicle.csn:
            update_data["csn"] = vehicle.csn
        if vehicle.list:
            update_data["list"] = vehicle.list
        if vehicle.send_time:
            update_data["send_time"] = vehicle.send_time

        dBSession.query(FloorVehicle).filter(FloorVehicle.id == vehicle.id) \
            .update(update_data, synchronize_session=False)
        return True

    # 根据id删除
    @classTransaction
    def delete_by_id(self, vehicle):
        update_data = {"updated_by": vehicle.updated_by, "updated_at": datetime.now(), "is_delete": 1}
        dBSession.query(FloorVehicle).filter(FloorVehicle.id == vehicle.id).update(update_data)
        return True

    @classTransaction
    def get_list(self, filters: set, offset: int = 0, limit: int = 15):
        res = {}
        res['page'] = {}
        res['page']['count'] = dBSession.query(FloorVehicle).filter(*filters).count()
        res['list'] = []
        res['page']['total_page'] = self.get_page_number(res['page']['count'], limit)
        res['page']['current_page'] = offset
        if offset != 0:
            offset = (offset - 1) * limit
        if res['page']['count'] > 0:
            res['list'] = dBSession.query(FloorVehicle).filter(*filters).offset(offset).limit(limit).all()
            res['list'] = [c.to_dict() for c in res['list']]
        return res

    @classTransaction
    def get_all(self):
        vehicle_list = dBSession.query(FloorVehicle).filter(FloorVehicle.is_delete == 0).all()
        vehicle_list = [c.to_dict() for c in vehicle_list]
        return vehicle_list

    @classTransaction
    def insert_vehicle_if_not_exists(self, vehicle):
        # 检查vin是否已存在
        existing_vehicle = dBSession.query(FloorVehicle).filter(FloorVehicle.vin == vehicle.vin).first()
        if not existing_vehicle:
            # 如果不存在，则创建新记录
            vehicle.created_at = datetime.now()
            dBSession.add(vehicle)
            print(f"新增了vin为{vehicle.vin}的车辆记录")
        else:
            print(f"vin为{vehicle.vin}的车辆记录已存在，跳过插入")

    @classTransaction
    def get_next_vehicle(self, vehicle):
        # 查找 id 大于当前车辆 id 的下一条记录
        next_vehicle = dBSession.query(FloorVehicle).filter(FloorVehicle.id > vehicle.id).order_by(
            FloorVehicle.id).first()
        return next_vehicle

    @classTransaction
    def get_number(self, start_vehicle, end_vehicle):
        return dBSession.query(FloorVehicle).filter(
            *[FloorVehicle.id >= start_vehicle.id, FloorVehicle.id <= end_vehicle.id]).count()
